/*
 * 上海远境文化传媒有限公司 版权所有
 */
package helper;

import play.Logger;
import play.mvc.Http;

/**
 * 浏览器相关辅助类.
 */
public class BrowserHelper {

    /**
     * 是否微信浏览器.
     * <p>
     * 获取微信内置浏览器，通过识别 MicroMessenger 这个关键字来确定是否微信内置的浏览器。
     */
    public static boolean isWeixinBrowser() {
        Http.Request request = Http.Request.current();

        if ("true".equals(request.params.get("mockWeixin"))) {
            return true;
        }

        if (request.headers.get("user-agent") == null) {
            return false;
        }

        String agent = request.headers.get("user-agent").value();
        Logger.debug("LOG01300: check isWeixinBrowser, agent: %s", agent);
        return agent.contains("MicroMessenger");
    }

    /**
     * 低于iOS8的使用旧界面
     */
    public static boolean isBelowIOS8() {
        Http.Request request = Http.Request.current();

        if (request.headers.get("user-agent") == null) {
            return false;
        }

        String agent = request.headers.get("user-agent").value();
        Logger.debug("LOG01300: check isWeixinBrowser, agent: %s", agent);

        boolean isBelowIOS8 = agent.contains("iPhone") && (agent.contains("iPhone OS 8") || agent.contains("iPhone OS 7") || agent.contains("iPhone OS 6"));
        if (isBelowIOS8) {
            Logger.info("LOG02723: 低于iOS8的使用旧界面 agent = " + agent);
        }
        return isBelowIOS8;
    }


    /**
     * 是否钉钉内置浏览器.
     * <p>
     * 获取钉钉内置浏览器，通过识别 MicroMessenger 这个关键字来确定是否微信内置的浏览器。
     */
    public static boolean isDingtalkBrowser() {
        Http.Request request = Http.Request.current();

        if (request.headers.get("user-agent") == null) {
            return false;
        }

        String agent = request.headers.get("user-agent").value();
        Logger.info("LOG91392: check isDingtalkBrowser, agent: %s", agent);
        return agent.contains("DingTalk") || agent.contains("AliApp");
    }
}
